PATH = "../../files/md5Model/model/"
LUAPATH = "D:/ProjectEngine/files/luafiles/"
package.path = "LUAPATH..?.lua"

dofile( LUAPATH.."Vector.lua" )
dofile( LUAPATH.."Key.lua" )
dofile( LUAPATH.."Model.lua" )
dofile( LUAPATH.."ctrlmethod.lua" )

--c functions
keycontrol 			 = utility.GetKeyState
mousecontrol 		 = utility.GetMouseState
intersect 			 = utility.GetIntersectVec3
getterrainheight = utility.GetTerrainHeight


--global vals
RotateBaseDir  		= Vector:New( { x = 1, y = 0, z = 0 } )
InitCameraPos 	 	= { x = 800, y = 1131.37, -800 }
HeightDisabled 		= 100.0 


ModelConfig = { 
	ModelTemplate.hellknight,		
	ModelTemplate.archvile,
	--ModelTemplate.player,
}
ModelConfig.size = #ModelConfig
ModelMgr = { size = ModelConfig.size, }


function getRolePos()
	local o = ModelMgr[ 1 ]
	
	if o ~= nil then	
		return o.pos.x, o.pos.y, o.pos.z
	else	
		return InitCameraPos
	end
end

function initmodel()
	for i, o in ipairs( ModelConfig ) do
		ModelMgr[ i ] = Model:New( o )	
	end
end

function update( dt )
	--control role
	local o = ModelMgr[ 1 ]
	local predest = o.dest

	local diff = o.dest - o.pos
	local prediff = predest - o.pos

	if diff.magnitude > 100.0 then 
		o:playanim( "walk" )
	else 
		o:playanim( "idle" )
	end

	if mousecontrol( Mouse.RIGHT ) then
		local dest 		= setdirbypick( o )	
		--to update animation only the new dest is quite different from the pre one
		diff = predest - dest
		if diff.magnitude > 50.0 then
			o:playanim( "idle" )
		end
	elseif keycontrol( Key.Q ) then
			o:playanim( "pain" )	
	end
end


